package chis.dip.fragments;

import java.io.IOException;
import java.util.List;

import chis.dip.map.R;
import chis.dip.model.MapFeature;
import chis.dip.model.MapFeatureManager;
import chis.dip.util.AndroidTools;
import chis.dip.util.LocationData;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class MapManager implements GooglePlayServicesClient.ConnectionCallbacks, 
GooglePlayServicesClient.OnConnectionFailedListener, GoogleMap.OnMapLongClickListener {

	private GoogleMap mMap;
	private MapFeatureManager features;
	private Activity a;
	private final String MARKER_ICON_DIRECTORY = "markers/";
	private LocationClient mLocationClient;
	private Location mCurrentLocation;
	private LatLng myLocation;

	public MapManager(Activity activity) {
		a = activity;
		mMap = ((MapFragment) a.getFragmentManager().findFragmentById(R.id.map)).getMap();
		initialiseMap();
	}

	private void initialiseMap() {
		mMap.setMyLocationEnabled(true);
		mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		mMap.setOnMapLongClickListener(this);
		features = new MapFeatureManager(a.getAssets());
		addFeaturesToMap();
		setStartCamera();
	}

	private void setStartCamera() {
		mLocationClient = new LocationClient(a, this, this);	
		mLocationClient.connect();
	}

	private void addFeaturesToMap() {
		List<MapFeature> l = features.getMapFeatures();
		for (MapFeature mf : l) {
			mMap.addMarker(MakeMapMarkerOptions(mf.getPosition(),mf.getName(),mf.getDescription(),mf.getFeatureType().getMarkerIconFileName()));
		}

	}

	private MarkerOptions MakeMapMarkerOptions(LatLng position,String title,String description,String markerIconFileName) {	
//		boolean exists = false;
//		try {
//			if(a.getAssets().open(MARKER_ICON_DIRECTORY + markerIconFileName) != null) {
//				exists = true;
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
//		BitmapDescriptor bd = BitmapDescriptorFactory.fromAsset("markerimages/filename.png");
//		if (bd == null) {
//			// doSomething...
//		}
				
		BitmapDescriptor bd = AndroidTools.assetExists(a, MARKER_ICON_DIRECTORY + markerIconFileName) ?
				BitmapDescriptorFactory.fromAsset(MARKER_ICON_DIRECTORY + markerIconFileName) :
			    BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA);
		
		Log.d("rakky", "1 is it null " + (bd == null) + markerIconFileName);
		
		MarkerOptions mo = new MarkerOptions()
		.position(position)
		.title(title)
		.snippet(description)
		.icon(bd);
		return mo;
	}

	private void updateMyLocationMarkerIcon() {
		//Log.d("********My Location:", Double.toString(myLocation.latitude) + " " + Double.toString(myLocation.longitude));
		addMarkerToMap(MakeMapMarkerOptions(myLocation, "You are here!", "Happy dance!", "none.png"));
	}

	private void addMarkerToMap(MarkerOptions mOptions) {
		//Log.d("***********MMMMM OPTIONS: ", mOptions.getIcon());
		//Log.d("MAP MAP MAP: ", mMap.getMyLocation().toString());
		mMap.addMarker(mOptions);
		
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.CHISHOLM_AT_CRANBOURNE)
//		.title("BLAAAAAAAAA")
//		.snippet("New Holland Drive Cranbourne 3177"));
	}

	public GoogleMap getmMap() {
		return mMap;
	}

	public void setmMap(GoogleMap mMap) {
		this.mMap = mMap;
	}
	

	@Override
	public void onConnected(Bundle arg0) {
		mCurrentLocation = mLocationClient.getLastLocation();
		myLocation = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
		mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,10f));
		updateMyLocationMarkerIcon();
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMapLongClick(LatLng arg0) {
		addMarkerToMap(new MarkerOptions()
		.position(arg0)
		.title("here"));		
	}



}
